import time

import pymysql
import pymongo
from loguru import logger


class AmazonSeleniumPipeline:
    start = None

    def open_spider(self, spider):
        start = time.time()
        self.client = pymongo.MongoClient()
        self.collection = self.client['py_spider']["tb_amazon_1"]

    def process_item(self, item, spider):
        logger.info(f"数据:{item}插入中...")
        try:
            self.collection.insert_one(dict(item))
            logger.info(f"数据插入成功!!!")
        except Exception as e:
            logger.exception(f"数据插入失败,{type(e)}, {e}")
        return item

    def close_spider(self, spider):
        logger.debug(f"程序运行时间{time.time() - self.start}")
        self.client.close()
